﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace MsForgeReboot.Web.Attributes
{
    using System.Web.Mvc;

    /// <summary>
    /// Specify which properties to ignore durin model validation
    /// </summary>
    public class IgnorePropertyValidation : ActionFilterAttribute
    {
        private readonly string[] propertyNames;

        public IgnorePropertyValidation(params string[] propertyNames) {
            this.propertyNames = propertyNames;
        }

        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            var modelState = filterContext.Controller.ViewData.ModelState;
            
            foreach (var propertyName in propertyNames) {
                modelState[propertyName].Errors.Clear();
            }
        }
    }
}